//
//  KKBLEDFUHelper.h
//
//  Created by zorro on 2017/11/13.
//  Copyright © 2017年 zorro. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import "Utility.h"
#import "Header.h"

typedef enum {
    KKBLEDFUHelperUpdateing     = 2,              // 更新中
    KKBLEDFUHelperUpdateSuccess = 3,              // 更新成功
    KKBLEDFUHelperUpdateFail    = 4,              // 更新失败
    KKBLEDFUHelperNoUpdate = 0,                   // 没更新
    KKBLEDFUHelperUpdateNoUpdateFile = 1,         // 没有更新文件
} KKBLEDFUHelperUpdateStatus;

typedef void (^KKBLEDFUHelperUpdate)(KKBLEDFUHelperUpdateStatus state, NSInteger number);

@interface KKBLEDFUHelper : NSObject

@property (nonatomic, strong) KKBLEDFUHelperUpdate updateBlock;

@property (nonatomic, strong) CBPeripheral     *updatePeripheral; //  正在更新的外围设备
@property (nonatomic, strong) CBCharacteristic *controlPointChar;
@property (nonatomic, strong) CBCharacteristic *packetChar;
@property (nonatomic, strong) CBCharacteristic *versionChar;

@property (nonatomic, strong) NSData *binFileData;                //  升级的数据包。

@property (nonatomic, assign) BOOL isAllowUpdate;

@property (nonatomic, assign) NSUInteger binFileSize;

@property (nonatomic, assign) int writingPacketNumber;
@property (nonatomic, assign) int numberOfPackets;
@property (nonatomic, assign) int bytesInLastPacket;

@property (nonatomic, strong) NSDate         *startTime;
@property (nonatomic, strong) NSDate         *finishTime;
@property (nonatomic, assign) NSTimeInterval uploadTimeInSeconds;

@property (nonatomic, assign) BOOL isFoundControlPointChar;
@property (nonatomic, assign) BOOL isFoundPacketChar;
@property (nonatomic, assign) BOOL isFoundVersionChar;

@property (nonatomic, strong) NSURL *firmwareFileMetaData;

@property struct DFUResponse dfuResponse;
AS_SINGLETON(KKBLEDFUHelper)
// 开始升级
- (void)performDFUOnFileWithMetaData;
// 接受来自控制中心的信息.
- (void)processDFUResponse:(uint8_t *)data;
- (void)onReadDfuVersion:(NSInteger)version;

@end

